// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:56:51
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Property\PBARParser.cpp
 */
#include "PBARParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPBARService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (PBAR);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PBAR/PBAR.md>
*/
enum PBARKEY : std::uint8_t
{
    PBARKEY_PID = 2,   // 属性识别号 (Property Identification Number)
    PBARKEY_MID = 3,   // 材料识别号 (Material Identification Number)
    PBARKEY_A   = 4,   // 截面积 (Area of bar cross section)
    PBARKEY_I1  = 5,   // 惯性矩 I1 (Area moment of inertia Izz)
    PBARKEY_I2  = 6,   // 惯性矩 I2 (Area moment of inertia Iyy)
    PBARKEY_J   = 7,   // 扭转常数 (Torsional constant)
    PBARKEY_NSM = 8,   // 每单位长度的非结构质量 (Nonstructural mass per unit length)
    PBARKEY_C1  = 12,  // 应力恢复系数 C1 (Stress recovery coefficient C1)
    PBARKEY_C2  = 13,  // 应力恢复系数 C2 (Stress recovery coefficient C2)
    PBARKEY_D1  = 14,  // 应力恢复系数 D1 (Stress recovery coefficient D1)
    PBARKEY_D2  = 15,  // 应力恢复系数 D2 (Stress recovery coefficient D2)
    PBARKEY_E1  = 16,  // 应力恢复系数 E1 (Stress recovery coefficient E1)
    PBARKEY_E2  = 17,  // 应力恢复系数 E2 (Stress recovery coefficient E2)
    PBARKEY_F1  = 18,  // 应力恢复系数 F1 (Stress recovery coefficient F1)
    PBARKEY_F2  = 19,  // 应力恢复系数 F2 (Stress recovery coefficient F2)
    PBARKEY_K1  = 22,  // 剪切面积系数 K1 (Shear area factor K1)
    PBARKEY_K2  = 23,  // 剪切面积系数 K2 (Shear area factor K2)
    PBARKEY_I12 = 24   // 交叉惯性矩 I12 (Cross-inertia I12)
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParsePBAR (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存PBAR数据的临时结构体对象
    SG::DataStructure::FEM::PBARData tempData;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempData.m_domainId = 1;

    int curIDKey = 1;  // 当前处理的条目索引

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前条目是否为空
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        // 根据条目ID，将解析后的数值存入对应的PBAR字段
        switch (curIDKey)
        {
        case PBARKEY_PID:
            tempData.m_id = convertToI64 (curStrKey);    // 解析PID并赋值
            break;
        case PBARKEY_MID:
            tempData.m_mId = convertToI64 (curStrKey);   // 解析MID并赋值
            break;
        case PBARKEY_A:
            tempData.m_A = convertToReal (curStrKey);    // 解析截面积A并赋值
            break;
        case PBARKEY_I1:                                 // I1 =Izz elem
            tempData.m_I1 = convertToReal (curStrKey);   // 解析惯性矩I1并赋值
            break;
        case PBARKEY_I2:                                 // I2 =Iyy elem
            tempData.m_I2 = convertToReal (curStrKey);   // 解析惯性矩I2并赋值
            break;
        case PBARKEY_J:                                  // J =Ixx elem
            tempData.m_J = convertToReal (curStrKey);    // 解析扭转常数J并赋值
            break;
        case PBARKEY_NSM:
            tempData.m_NSM = convertToReal (curStrKey);  // 解析非结构质量NSM并赋值
            break;
        case PBARKEY_C1:
            tempData.m_C1 = convertToReal (curStrKey);   // 解析应力恢复系数C1并赋值
            break;
        case PBARKEY_C2:
            tempData.m_C2 = convertToReal (curStrKey);   // 解析应力恢复系数C2并赋值
            break;
        case PBARKEY_D1:
            tempData.m_D1 = convertToReal (curStrKey);   // 解析应力恢复系数D1并赋值
            break;
        case PBARKEY_D2:
            tempData.m_D2 = convertToReal (curStrKey);   // 解析应力恢复系数D2并赋值
            break;
        case PBARKEY_E1:
            tempData.m_E1 = convertToReal (curStrKey);   // 解析应力恢复系数E1并赋值
            break;
        case PBARKEY_E2:
            tempData.m_E2 = convertToReal (curStrKey);   // 解析应力恢复系数E2并赋值
            break;
        case PBARKEY_F1:
            tempData.m_F1 = convertToReal (curStrKey);   // 解析应力恢复系数F1并赋值
            break;
        case PBARKEY_F2:
            tempData.m_F2 = convertToReal (curStrKey);   // 解析应力恢复系数F2并赋值
            break;
        case PBARKEY_K1:
            tempData.m_K1 = convertToReal (curStrKey);   // 解析剪切面积系数K1并赋值
            break;
        case PBARKEY_K2:
            tempData.m_K2 = convertToReal (curStrKey);   // 解析剪切面积系数K2并赋值
            break;
        case PBARKEY_I12:
            tempData.m_I12 = convertToReal (curStrKey);  // 解析惯性矩I12并赋值
            break;
        default:
            break;
        }
        curIDKey++;
    }

    // 将解析后的PBAR数据通过数据库服务工厂的PBARService服务添加到数据库中
    return dbServiceFactory->get<IPBARService> ()->Add (tempData);
}